﻿using Spire.Pdf;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmPdfToWord : Form
    {
        public FrmPdfToWord()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 打开PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "打开PDF";
            fileDialog.Filter = "pdf文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string name = fileDialog.FileName;
                txtFilePath.Text = name;
            }
        }

        /// <summary>
        /// 选择保存路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "选择保存路径";
            saveFile.Filter = "word文件|*.doc;*.docx";
            saveFile.DefaultExt = ".docx";
            saveFile.FileName = "转为word的文档.docx";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFile.FileName;
            }
        }

        /// <summary>
        /// 开始转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartCsharp_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请选择要操作的pdf文件！");
                return;
            }
            //初始化PdfDocument实例
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            //加载PDF文档
            doc.LoadFromFile(txtFilePath.Text);
            //保存为Doc格式文档
            doc.SaveToFile(txtSavePath.Text, FileFormat.DOCX);
            MessageBox.Show("完成");
        }



        /// <summary>
        /// 打开转换后的Word
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenWord_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.File.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件
                System.Diagnostics.Process.Start(txtSavePath.Text);
            }
        }


        /// <summary>
        /// 开始PDF转Word（调用Python方法）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartPy_Click(object sender, EventArgs e)
        {
            var filePath = txtFilePath.Text;
            var savePath = txtSavePath.Text;
            if (string.IsNullOrEmpty(filePath))
            {
                MessageBox.Show("请添加要操作的pdf文档！");
                return;
            }
            if (string.IsNullOrEmpty(savePath))
            {
                MessageBox.Show("请选择文件保存路径！");
                return;
            }
            if (!File.Exists("WordToPdf.exe"))   //Python转换程序文件不存在
            {
                MessageBox.Show("文件缺失，请检查！");
                return;
            }

            string args = txtFilePath.Text + " " + savePath;


            // 文件存在删除
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }

            try
            {
                Process myProcess = new Process();
                // 调用当前目录下的PdfToWord.exe（Python程序）进行转换
                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("PdfToWord.exe", args);
                myProcessStartInfo.CreateNoWindow = true;
                myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                myProcess.StartInfo = myProcessStartInfo;
                myProcess.Start();

                while (!myProcess.HasExited)
                {
                    myProcess.WaitForExit();
                }

                _ = myProcess.ExitCode;
                myProcess.Dispose();
                myProcess.Close();

                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 开始拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.txtFilePath.Cursor = Cursors.Arrow; //指定鼠标形状   
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


        /// <summary>
        /// 接收拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (Path.GetExtension(path).ToLower() == ".pdf")
            {
                this.txtFilePath.Text = path;
            }
            this.txtFilePath.Cursor = Cursors.IBeam; //还原鼠标形状 
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
